package com.huizu.project.apiController;

import com.huizu.common.annotation.Anonymous;
import com.huizu.common.annotation.Log;
import com.huizu.common.core.controller.BaseController;
import com.huizu.common.core.domain.AjaxResult;
import com.huizu.common.core.domain.entity.SysUser;
import com.huizu.common.core.page.TableDataInfo;
import com.huizu.common.enums.BusinessType;
import com.huizu.common.utils.poi.ExcelUtil;
import com.huizu.project.config.TerAspect;
import com.huizu.project.config.UserAspect;
import com.huizu.project.domain.ProUser;
import com.huizu.project.domain.ProUserTer;
import com.huizu.project.service.IProUserTerService;
import com.huizu.project.utils.TokenUtils;
import com.huizu.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 教师用户Controller
 * 
 * @author zhangmx
 * @date 2024-05-25
 */
@Anonymous
@RestController
@RequestMapping("/api/prouserter")
public class ApiProUserTerController extends BaseController
{
    @Autowired
    private IProUserTerService proUserTerService;

    @Autowired
    private ISysUserService iSysUserService;

    /**
     * 获取教师用户详细信息
     */
    @TerAspect
    @PostMapping(value = "/getById")
    public AjaxResult getInfo(HttpServletRequest request)
    {
        String token = request.getHeader(TokenUtils.getAppTokenHeader());
        String userId = TokenUtils.parseToken(token);//获取用户id
        return success(iSysUserService.selectUserById(Long.valueOf(userId)));
    }


    /**
     * 修改教师用户
     */
    @TerAspect
    @PostMapping("/update")
    public AjaxResult update(@RequestBody SysUser sysUser, HttpServletRequest request)
    {
        String token = request.getHeader(TokenUtils.getAppTokenHeader());
        String userId = TokenUtils.parseToken(token);//获取用户id
        sysUser.setUserId(Long.valueOf(userId));
        return toAjax(iSysUserService.updateUserImage(sysUser));
    }

    @TerAspect
    @PostMapping("/updatePassword")
    public AjaxResult updatePassword(@RequestBody ProUserTer proUser, HttpServletRequest request){
        String token = request.getHeader(TokenUtils.getAppTokenHeader());
        String userId = TokenUtils.parseToken(token);//获取用户id
        proUser.setUserId(userId);
        return proUserTerService.updatePassword2(proUser);
    }

    @TerAspect
    @PostMapping("/listMessage")
    public AjaxResult listMessage(HttpServletRequest request, Map<String,Object> map){
        String token = request.getHeader(TokenUtils.getAppTokenHeader());
        String userId = TokenUtils.parseToken(token);//获取用户id
        map.put("userId",userId);
        return proUserTerService.listMessage(map);
    }

    /**
     * 解除绑定
     * @param request
     * @return
     */
    @PostMapping("/relieveBinding")
    public AjaxResult relieveBinding(HttpServletRequest request){
        String token = request.getHeader(TokenUtils.getAppTokenHeader());
        String userId = TokenUtils.parseToken(token);//获取用户id
        SysUser sysUser = new SysUser();
        sysUser.setUserId(Long.parseLong(userId));
        return proUserTerService.relieveBinding(sysUser);
    }

}
